package cn.wolfcode.crm.service.imp;


import cn.wolfcode.crm.domain.ProductSort;
import cn.wolfcode.crm.domain.ProductSortTwo;
import cn.wolfcode.crm.mapper.ProductSortMapper;
import cn.wolfcode.crm.mapper.ProductSortTwoMapper;
import cn.wolfcode.crm.service.IProductSortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class ProductSortServiceImpl implements IProductSortService {
    @Autowired
    private ProductSortMapper productSortMapper;
    @Autowired
    private ProductSortTwoMapper productSortTwoMapper;
    @Override

    public void deleteByPrimaryKey(Long id) {
        if (id != null) {
            ProductSort productSort = productSortMapper.selectByPrimaryKey(id);
            //维护与明细之间的关系  通过sn删除
            productSortTwoMapper.deleteBySn(productSort.getSn());
            productSortMapper.deleteByPrimaryKey(id);
        }
    }

    @Override

    public void saveOrUpdate(ProductSort entity) {
        if (entity.getId() != null) {
            //更新操作
            productSortMapper.updateByPrimaryKey(entity);
            //维护与明细之间的关系 通过sn删除
            productSortTwoMapper.deleteBySn(entity.getSn());
        }else {
            productSortMapper.insert(entity);
        }
        //更新与新增都需要插入
        if (entity.getProductSortTwos() != null) {

            for (ProductSortTwo two : entity.getProductSortTwos()) {
                //插入many方对象
                productSortTwoMapper.insert(two);
            }
        }
    }

    @Override
    public ProductSort selectByPrimaryKey(Long id) {
        ProductSort dept = productSortMapper.selectByPrimaryKey(id);
        return dept;

    }

    @Override
    public List<ProductSort> selectAll() {
        List<ProductSort> depts = productSortMapper.selectAll();
        return depts;
    }
}
